// 公用方法
import {
	houseCheck
} from '@/pages/utils/api.js'
const checkHouseState = async () => {
	let res = await houseCheck()
	// console.log(res);
	if (res.errCode === 10000) {
		const arr = res.data.list.filter(item => {
			if (item.state === 2) {
				uni.setStorageSync('buildingState', 2)
				return item
			} else if (item.state === 1) {
				uni.setStorageSync('buildingState', 1)

			}

		})
		if(res.data.list===''){
			uni.setStorageSync('buildingState', 0)
		}
		uni.setStorageSync('buidings', arr)
	}
}

/*
 * toPage 根据认证状态跳转对应的页面-路由
 * @params url  页面路由
 */
const toPage = (url) => {
	const buildingState = uni.getStorageSync('buildingState') // 0 1 2
	console.log(buildingState);
	// switch(buildingState) {
	// 	case 1: 
	// 	uni.navigateTo({
	// 		url:'/pages/user/house/houseWaiting/houseWaiting'
	// 	})
	// 	break;
	// case 2:
	// 	uni.navigateTo({
	// 		url
	// 	})
	// break;	
	// }
	if (buildingState === 0) {
		uni.navigateTo({
			url: '/pages/mine/houuseList/houseList'
		})
	} else if(buildingState === 1) {
		uni.navigateTo({
			url: '/pages/mine/houseWaiting/houseWaiting'
		})
	}else{
		uni.navigateTo({
			url
		})
	}
}
// 图片上传
const uploadImg = (title='图片上传成功', sizeType = 'compressed') => {
	return new Promise(async (resolve, reject) => {
		const [, {tempFilePaths}] = await uni.chooseImage({
			count: 1, // 选择图片数量
			sizeType: [sizeType], // 使用压缩图片
			sourceType: ['album','camera'] // 本地相册/相机
		})
		await uni.showLoading({
			title: '图片正在上传...',
			mask: true
		})
		await uni.uploadFile({
			url: config.uploadUrl,
			name: 'file',
			filePath: tempFilePaths[0],
			header: {
				Authorization: uni.getStorageSync('token') || ''	
			},
			success: res => {
				const result = JSON.parse(res.data)
				if (result.errCode === 10000) {
					uni.showToast({
						title
					})
					resolve({url:result.data})
					uni.hideLoading()
				}
			}
		})
	})
}
export default {
	checkHouseState,
	toPage,
	uploadImg
}
